package com.jss.lanouhn.fristapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.adapters.SearchWordsAdapter;
import com.jss.lanouhn.fristapp.modles.SearchWords;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SearchActivity extends AppCompatActivity implements View.OnClickListener {
    private RecyclerView recyclerView;
    private SearchWords words;
    private SearchWordsAdapter adapter;
    private String text, url = "http://api.liwushuo.com/v2/search/hot_words";
    private LinearLayout linearLayout;
    private ImageView iv_forBack;
    private EditText editText;
    private TextView tv_search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    tv_search.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            text = editText.getText().toString();
                            Intent intent = new Intent(SearchActivity.this, SearchThingsActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("name", text + "");
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                } else {
                    tv_search.setClickable(false);
                    Toast.makeText(SearchActivity.this, "请输入想要搜索的内容", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.rv_search);
        linearLayout = (LinearLayout) findViewById(R.id.ll_search_bottom);
        iv_forBack = (ImageView) findViewById(R.id.btn_back_credit);
        tv_search = (TextView) findViewById(R.id.tv_search);
        editText = (EditText) findViewById(R.id.et_search);
        editText.requestFocus();

        linearLayout.setOnClickListener(this);
        iv_forBack.setOnClickListener(this);
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        words = gson.fromJson(result, SearchWords.class);
                        if (words.getData().getHot_words().size() > 0)
                            handler.sendEmptyMessage(1);
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                recyclerView.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
                adapter = new SearchWordsAdapter(SearchActivity.this, words);
                recyclerView.setAdapter(adapter);
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_search_bottom:
                startActivity(new Intent(SearchActivity.this, SearchContentActivity.class));
                break;
            case R.id.btn_back_credit:
                finish();
                break;
        }
    }
}
